package com.mz.ozy.activity.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.mvp.presenter.ForgetPwdPresenter;
import com.mz.ozy.mvp.view.ForgetPwdContract;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPwdActivity extends BaseActivity<ForgetPwdContract.View, ForgetPwdPresenter> implements ForgetPwdContract.View {

    @BindView(R.id.usernameEt)
    EditText usernameEt;
    @BindView(R.id.codeEt)
    EditText codeEt;
    @BindView(R.id.pwdEt1)
    EditText pwdEt1;
    @BindView(R.id.pwdEt2)
    EditText pwdEt2;
    @BindView(R.id.btGetCode)
    TextView mBtGetCode;//获取验证码
    @BindView(R.id.user)
    EditText user;
    private boolean canClick = true;//是否可以点击
    private MyCount mc;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    canClick = true;
                    mBtGetCode.setText(R.string.get_code);


                    break;
                case 2:
                    mBtGetCode.setText(msg.obj + "s");
                    break;
                default:
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText(R.string.forget_pwd1);
        tvTitle.setVisibility(View.GONE);
        mc = new MyCount(60 * 1000, 1000);
    }

    @Override
    public void getCodeSuccess(Object o) {
        canClick = false;
        ToastUtils.showToast(getString(R.string.str_send_sms_success));
        mc.start();

    }

    @Override
    public void obtainForgetPwdSuccess(Object o) {
        ToastUtils.showToast(getString(R.string.forget_pwd_success));
        finish();

    }

    /*定义一个倒计时的内部类*/
    class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            handler.sendEmptyMessage(1);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Message msg = new Message();
            msg.what = 2;
            msg.obj = millisUntilFinished / 1000;
            handler.sendMessage(msg);

        }
    }

    @OnClick({R.id.btGetCode, R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btGetCode:
                if (!checkParams(1)) return;
                if (!canClick) {
                    return;
                }
                mPresenter.getCode(this, user.getText().toString().trim(), usernameEt.getText().toString().trim());
                break;
            case R.id.bt_confirm:
                if (!checkParams(2)) return;
                mPresenter.obtainForgetPwd(this,user.getText().toString().trim(), usernameEt.getText().toString().trim(), codeEt.getText().toString(), pwdEt1.getText().toString());
                break;
        }
    }

    public boolean checkParams(int tag) {
        final String username = usernameEt.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            ToastUtils.showToast(getString(R.string.str_input_phone_hint));
            return false;
        }
        final String use = user.getText().toString().trim();
        if (TextUtils.isEmpty(use)) {
            ToastUtils.showToast(getString(R.string.hint_user));
            return false;
        }
        if (tag == 2) {
            if (TextUtils.isEmpty(codeEt.getText().toString().trim())) {
                ToastUtils.showToast(getString(R.string.str_sms_code));
                return false;
            }
            //登录密码
            String pwd1 = pwdEt1.getText().toString().trim();
            String pwd2 = pwdEt2.getText().toString().trim();
            if (TextUtils.isEmpty(pwd1) || pwd1.length() < 8 || !Utils.ispsd(pwd1)) {
                ToastUtils.showToast(R.string.str_input_newpwd_hint);
                return false;
            }
            if (TextUtils.isEmpty(pwd2)) {
                ToastUtils.showToast(getString(R.string.str_again_input_login_pwd_hint));
                return false;
            }
            if (!pwd1.equals(pwd2)) {
                ToastUtils.showToast(getString(R.string.str_loginpwd_inconsistent));
                return false;
            }
        }
        return true;
    }

    @Override
    public void handleFailResponse(NetBean bean) {
        super.handleFailResponse(bean);
        canClick = true;
    }
}
